package com.shop2cn.iapi.wxcomponent.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.wxcomponent.parameter.ReleaseBean;
import com.shop2cn.iapi.wxcomponent.service.ReleaseCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.database.model.Mchapp;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*发布独立小程序
*FreeMaker Template自动生成代码
*/
@TestSuite
@Ignore
//接口重新命名-这个接口不做实际的release 发布在回调接口
public class Ts_Release{
	private static ReleaseBean releaseBean;
	private static ReleaseCall releaseCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发布独立小程序");
	}

	@Before
	public void caseUp() {
		releaseBean=new ReleaseBean();
		releaseCall=new ReleaseCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(releaseCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Release_001() {
		Logger.start(true,"发布独立小程序");
		try {
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			Integer mchid=13001;
			DefGodMchWapper.deleteMchappByMchid(mchid);
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ConfirmAuditMchApp(mchid);
			WxComponentService.AppCallBackAuditSuccess(mchid, appid);
			
			releaseBean.setMchId(mchid);
			releaseCall.setData(releaseBean);
			releaseCall.callService();			
			Logger.verifyEquals("200", releaseCall.getString("code"), "验证返回code");
			Logger.verifyEquals(true,DefGodMchWapper.selectMchappByExample(mchid).get(0).getIsPublished(), "验证Published");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Release_002() {
		Logger.start(true,"不输入mchid失败");
		try {
			releaseCall.setData(releaseBean);
			releaseCall.callService();			
			Logger.verifyEquals("400", releaseCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Release_003() {
		Logger.start(true,"输入错误MchId失败");
		try {
			releaseBean.setMchId(-1);
			releaseCall.setData(releaseBean);
			releaseCall.callService();			
			Logger.verifyEquals("400", releaseCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Release_004() {
		Logger.start(true,"发布独立小程序失败");
		try {
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			Integer mchid=13001;
			DefGodMchWapper.deleteMchappByMchid(mchid);
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ConfirmAuditMchApp(mchid);
			WxComponentService.AppCallBackAuditSuccess(mchid, appid);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-release-Fail");
			PayGateWayCallService.addMock(releaseCall, m);
			releaseBean.setMchId(mchid);
			releaseCall.setData(releaseBean);
			releaseCall.callService();			
			Logger.verifyEquals("200", releaseCall.getString("code"), "验证返回code");
			Logger.verifyEquals(false,DefGodMchWapper.selectMchappByExample(mchid).get(0).getIsPublished(), "验证Published");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}